#plot circuit control, 1986-2005
detach()
attach.all(partisan.comp.merge, overwrite = T)

circuit.label = c("1st", "2nd", "3rd", "4th", "5th", "6th", "7th","8th", "9th","10th", "11th", "D.C.")
#identify years of dem control
circuit.year <- paste(partisan.comp.merge$year, partisan.comp.merge$circuit)
print(circuit.year[year >=1986 & year <= 2005 & dem.circuit==1])
# [1] "1986 1"  "1997 2"  "1998 2"  "1999 2"  "2000 2" 
# [6] "2001 2"  "2002 2"  "2003 2"  "2004 2"  "2005 2" 
#[11] "2002 3"  "1998 6"  "2000 6"  "2001 6"  "2002 6" 
#[16] "1986 9"  "1988 9"  "1998 9"  "1999 9"  "2000 9" 
#[21] "2001 9"  "2002 9"  "2003 9"  "2004 9"  "2005 9" 
#[26] "2001 10" "1986 11" "1987 11"

pdf("figure3b.pdf", width = 7.5, height = 6.5)

par( mar = c(2,2,1,.5))
point.size <- 1
line.width <- 2
left.panel.width <- .3 # control width of left-hand panel (see "widths" in layout command)
use.widths <- cbind(c(left.panel.width,2,2,2),c(left.panel.width,2,2,2), 
      c(left.panel.width,2,2,2), c(left.panel.width,2,2,2), c(left.panel.width,2,2,2))

layout(rbind(c(13, 1, 2,3), c(14,4,5,6), #order so that each year first, then y-axis labels, then x-axis lables
    c(15,7, 8,9), c(16,10,11,12)),
    widths = use.widths,    
    respect = F,
    heights = c(1,1,1,1,.15,.3))#make last rows smaller since they're labels, no graph
#layout.show(16)#this shows how layout looks in R Window (but must be commented out when creating PDF)

for (i in 1:12){
  keep <- year >= 1986 & year<=2005 & circuit == i
  plot(year[keep], gop.proportion[keep], type = "n", axes = F, main="",
  xlab = "", ylab = "", xlim = c(1986,2005), ylim = c(0, 1))
  #do shading for dem control 
  shade.color = "gray75"
  if (i == 1) polygon(x=c(1986, 1986, 1987, 1987), y=par()$usr[c(3,4,4,3)],col= shade.color, border=F)
  if (i == 2) polygon(x=c(1997, 1997, 2005, 2005), y=par()$usr[c(3,4,4,3)],col= shade.color, border=F)
  if (i == 3) polygon(x=c(2002, 2002, 2003, 2003), y=par()$usr[c(3,4,4,3)],col= shade.color, border=F)
  if (i == 6) polygon(x=c(1998,1998, 2002, 2002), y=par()$usr[c(3,4,4,3)],col= shade.color, border=F)
  if (i == 9) polygon(x=c(1986, 1986, 1987, 1987), y=par()$usr[c(3,4,4,3)],col= shade.color, border=F)
  if (i == 9) polygon(x=c(1988, 1988, 1989, 1989), y=par()$usr[c(3,4,4,3)],col= shade.color, border=F)
  if (i == 9) polygon(x=c(1998, 1998, 2005, 2005), y=par()$usr[c(3,4,4,3)],col= shade.color, border=F)
  if (i == 10) polygon(x=c(2001, 2001, 2002, 2002), y=par()$usr[c(3,4,4,3)],col= shade.color, border=F)
  if (i == 11) polygon(x=c(1986, 1986, 1988, 1988), y=par()$usr[c(3,4,4,3)],col= shade.color, border=F)
  points(year[keep], gop.proportion[keep], type="l")
  axis(1, at = c(1986:2005), labels = F)
  axis(1, at = c(1986,seq(1990,2005,5)), labels = c(1986,seq(1990,2005,5)), tcl = -1)
  axis(2, at = seq(0,1,.2), labels = c(0,".2", ".4",".6", ".8",1), las = 1)
  mtext(circuit.label[i],3,font=2, line = -1)
  abline(h=.5, lty =2, col = "green", lwd = 1.5)
   }
 
 #outside loop:  add labels
#first: y-axis (4 rows)
for (i in 1:4){
par(mar=c(0,0,0,0))
plot(.5,.5, xlim = c(.4, .7), ylim = c(.4, .7), type = "n", axes = F, xlab = "", ylab = "")#call empty plot to keep on same scale
text(.56, .575, "Proportion of\nRepublicans", cex = 1.3, srt = 90, xpd = NA)
}

dev.off()
